<?php defined('SYSPATH') or die('No direct script access.');

/**
 * Class Marketing_Seckill 秒杀
 */
class Controller_Pc_Api_V2_Marketing_Seckill extends Controller_Pc_Api_V2_Base
{
    // 数据请求结果
    private $result;

    /**
     * 参数验证对象
     * @var Request_Api_V2_Marketing_Seckill
     */
    private $validation;

    /**
     * 服务
     * @var Service_Api_Marketing_Seckill
     */
    private $service;

    public function before()
    {
        parent::before();

        if (!St_Functions::is_normal_app_install('seckill')) {
            $this->result['status'] = false;
            $this->result['msg']    = '秒杀应用未安装';
            $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
        }

        // 验证
        $this->validation = Request_Api_V2_Factory::create('marketing_seckill', $this->request_params);

        // 服务
        $this->service = new Service_Api_Marketing_Seckill();

        // 返回数据
        $this->result = array('status' => true, 'msg' => '操作成功', 'data' => array());
    }

    public function after()
    {
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
    }

    // 秒杀列表页
    public function action_list()
    {
        try {
            $this->validation->list()->check();

            $this->result['data'] = $this->service->list($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    //  秒杀详情页
    public function action_show()
    {
        try {
            $this->validation->show()->check();

            $this->result['data'] = $this->service->show($this->request_params);
        } catch (Exception $e) {
            $this->result['status'] = false;
            $this->result['msg']    = $e->getMessage();
        }
    }

    /**
     * @function 判断秒杀时间
     */
    public function action_judge_seckill_date()
    {
        $seckill_id     = $this->request_data->seckill_id;
        $use_start_date = $this->request_data->use_start_date;
        $use_end_date   = $this->request_data->use_end_date;
        if ($use_start_date && $use_end_date) {
            $res = DB::select()->from('seckill')->where("id={$seckill_id} and FIND_IN_SET({$use_start_date},suit_date)")->execute()->current();
            if (!$res) {
                $day    = ($use_end_date - $use_start_date) / 86400;
                $status = false;
                for ($i = 0; $i < $day; $i++) {
                    $date = $use_start_date + (($i + 1) * 86400);
                    $res  = DB::select()->from('seckill')->where("id={$seckill_id} and FIND_IN_SET({$date},suit_date)")->execute()->current();
                    if ($res) {
                        $status = true;
                        break;
                    }
                }
                if ($status) {
                    $this->result['status'] = true;
                    $this->result['msg']    = true;
                    $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
                } else {
                    $this->result['status'] = false;
                    $this->result['msg']    = '未找到数据';
                    $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
                }
            } else {
                $this->result['status'] = true;
                $this->result['msg']    = true;
                $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
            }
        } else if ($use_start_date && !$use_end_date) {
            $use_start_date = strtotime($use_start_date);
            $res            = DB::select()->from('seckill')->where("id={$seckill_id} and FIND_IN_SET({$use_start_date},suit_date)")->execute()->current();
            if ($res) {
                $this->result['status'] = true;
                $this->result['msg']    = true;
                $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
            } else {
                $this->result['status'] = false;
                $this->result['msg']    = '未找到数据';
                $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
            }
        } else {
            $this->result['status'] = false;
            $this->result['msg']    = '未找到数据';
            $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
        }
    }

    public function action_hotel_room()
    {
        $suit_id    = $this->request_data->suit_id;
        $hotel_id   = $this->request_data->hotel_id;
        $seckill_id = $this->request_data->is_seckill;
        if ($suit_id && $hotel_id) {
            $hotel_room = DB::select()->from('hotel_room')->where("hotelid={$hotel_id} and id={$suit_id}")->execute()->current();
            if ($hotel_room) {
                if ($seckill_id) {
                    $seckill = DB::select()->from('seckill')->where("id={$seckill_id}")->execute()->current();
                    if ($seckill) {
                        $hotel_room['pay_way']     = 1;
                        $hotel_room['limit_order'] = $seckill['purchase_limits'] > 0 && $seckill['purchase_limits'] < $seckill['number'] ? $seckill['purchase_limits'] : $seckill['number'];//库存;
                        $hotel_room['description'] = $seckill['describe'];
                        $hotel_room['bed_addable'] = 0;
                    } else {
                        $this->result['status'] = false;
                        $this->result['msg']    = '查询失败';
                        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
                    }
                }
                $hotel_room['productname'] = DB::select()->from('hotel')->where("id={$hotel_id}")->execute()->get('title');
                $hotel_room['productaid']  = DB::select()->from('hotel')->where("id={$hotel_id}")->execute()->get('aid');
                $hotel_room['title']       = $hotel_room['roomname'];
                $hotel_room['piclist']     = explode(',', $hotel_room['piclist']);
                $this->result['data']      = $hotel_room;
                $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
            }
        }
        $this->result['status'] = false;
        $this->result['msg']    = '查询失败';
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
    }

    public function action_car_suit()
    {
        $suit_id    = $this->request_data->suit_id;
        $car_id     = $this->request_data->car_id;
        $seckill_id = $this->request_data->is_seckill;
        if ($suit_id && $car_id) {
            $car_suit = DB::select()->from('car_suit')->where("carid={$car_id} and id={$suit_id}")->execute()->current();
            if ($car_suit) {
                if ($seckill_id) {
                    $seckill = DB::select()->from('seckill')->where("id={$seckill_id}")->execute()->current();
                    if ($seckill) {
                        $car_suit['paytype'] = 1;
                        $car_suit['number']  = $seckill['purchase_limits'] > 0 && $seckill['purchase_limits'] < $seckill['number'] ? $seckill['purchase_limits'] : $seckill['number'];//库存;
                        $car_suit['content'] = $seckill['describe'];
                    } else {
                        $this->result['status'] = false;
                        $this->result['msg']    = '查询失败';
                        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
                    }
                }
                $car_suit['productname'] = DB::select()->from('car')->where("id={$car_id}")->execute()->get('title');
                $car_suit['productaid']  = DB::select()->from('car')->where("id={$car_id}")->execute()->get('aid');
                $car_suit['title']       = $car_suit['suitname'];
                $this->result['data']    = $car_suit;
                $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key']);
            }
        }
        $this->result['status'] = false;
        $this->result['msg']    = '查询失败';
        $this->send_datagrams($this->client_info['id'], $this->result, $this->client_info['secret_key'], false, '查询失败', '查询失败');
    }
}